﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Empires.Infrastructure.Entities.Repository
{
    [Table("T_BlockSegment")]
    public class T_BlockSegment : IEntity<T_BlockSegment>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int TempId { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string BlockName { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string Segment { get; set; }

        public override T_BlockSegment Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_BlockSegment Find(System.Linq.Expressions.Expression<Func<T_BlockSegment, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.BlockSegment.FirstOrDefault(where);
                if (item != null)
                {
                    this.BlockName = item.BlockName;
                    this.Id = item.Id;
                    this.Segment = item.Segment;
                    this.TempId = item.TempId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_BlockSegment, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.BlockSegment.Any(where);
            }
        }
    }
}
